﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DevExpress.Web.Mvc;
using System.Data;
using EnterpriseFund.Common;
using DevExpress.Web.ASPxGridView;
using DevExpress.XtraReports.UI;
using DevExpress.XtraPrinting;

namespace EnterpriseFund.Controllers
{
    public class QueryAllowanceWaitListController : AdminBaseController
    {
        //
        // GET: /QueryAllowanceWaitList/


        public ActionResult Index()
        {
            BLL.Common CommonBLL = new BLL.Common();
            string year = (decimal.Parse(OperateSession.get(Common.GlobalConstant.PARAM_SYSYEAR))).ToString();
            string str = CommonBLL.GetGetRejectTaxSqlByYear(year);
            string strwhere1 = "";
            if (str != "")
            {
                strwhere1 = @"   NOT EXISTS (SELECT  ZSXH
         FROM fund.V_SB_ZSXX s WHERE (" + str + @")  AND
                                   r.FDataSource = s.FDataSource AND
                                   r.ZSXH = s.ZSXH ) and DATEDIFF(DAY,RKRQ,'" + year + "-01-01')<=0 AND DATEDIFF(DAY,RKRQ,'" + year + "-12-31')>=0 ";
            }
            else
            {
                strwhere1 = @" DATEDIFF(DAY,RKRQ,'" + year + "-01-01')<=0 AND DATEDIFF(DAY,RKRQ,'" + year + "-12-31')>=0 ";
            }
            string strwhere2 = " se>400000 ";
            ViewData["year"] = year;
            BLL.VSBZSXX VSBZSXXBLL = new BLL.VSBZSXX();
           // DataSet ds = VSBZSXXBLL.GetAllowanceWaitList(strwhere1, strwhere2);
            //return View(ds.Tables[0]);
            return View();
        }

        public ActionResult QueryAllowanceWaitListPartial()
        {
            DataSet ds = AllRequestToData();
            return PartialView("QueryAllowanceWaitListPartial",ds.Tables[0]);
        }

        private DataSet AllRequestToData()
        {
            string nsrmc = Request.Params["nsrmc"];
            string NSRBM = Request.Params["NSRBM"];
            string NSRSBH = Request.Params["NSRSBH"];
            string year = Request.Params["year"];
            string strwhere;
            string strwhere1;
            year = filters(nsrmc, NSRBM, NSRSBH, year, out strwhere, out strwhere1);


            BLL.VSBZSXX VSBZSXXBLL = new BLL.VSBZSXX();
            DataSet ds = VSBZSXXBLL.GetAllowanceWaitList(strwhere1, strwhere, year);
            return ds;
        }

        private static string filters(string nsrmc, string NSRBM, string NSRSBH, string year, out string strwhere, out string strwhere1)
        {
            /// 01 为工业企业
            strwhere = "((c.FEnterprisePropNo='01' and se>500000) or (c.FEnterprisePropNo<>'01' and se >400000) or (c.FEnterprisePropNo is null and se >400000))";

            if (year == "")
            {
                year = (decimal.Parse(OperateSession.get(Common.GlobalConstant.PARAM_SYSYEAR)) - 1).ToString();
            }

            if (NSRBM != "")
            {
                strwhere += " and NSRBM like  '%" + NSRBM + "%'";
            }
            if (NSRSBH != "")
            {
                strwhere += " and NSRSBH like  '%" + NSRSBH + "%'";
            }
            if (nsrmc != "")
            {
                strwhere += " and NSRMC like  '%" + nsrmc + "%'";
            }


            BLL.Common CommonBLL = new BLL.Common();
            string str = CommonBLL.GetGetRejectTaxSqlByYear(year);
            strwhere1 = "";
            if (str != "")
            {
                strwhere1 = @"   NOT EXISTS (SELECT  ZSXH
         FROM fund.V_SB_ZSXX s WHERE (" + str + @")  AND
                                   r.FDataSource = s.FDataSource AND
                                   r.ZSXH = s.ZSXH ) and DATEDIFF(DAY,RKRQ,'" + year + "-01-01')<=0 AND DATEDIFF(DAY,RKRQ,'" + year + "-12-31')>=0 ";
            }
            else
            {
                strwhere1 = @" DATEDIFF(DAY,RKRQ,'" + year + "-01-01')<=0 AND DATEDIFF(DAY,RKRQ,'" + year + "-12-31')>=0 ";
            }
            return year;
        }


        public ActionResult ExportToXls()
        {
            string nsrmc = Fetch.Post("hidnsrmc");
            string NSRBM = Fetch.Post("hidNSRBM");
            string NSRSBH = Fetch.Post("hidNSRSBH");
            string year = Fetch.Post("hidyear");
            string strwhere = "";

            strwhere = " ((c.FEnterprisePropNo='01' and se>500000) or (c.FEnterprisePropNo<>'01' and se >400000) or (c.FEnterprisePropNo is null and se >400000)) ";

            if (NSRBM != "")
            {
                strwhere += " and NSRBM like  '%" + NSRBM + "%'";
            }
            if (NSRSBH != "")
            {
                strwhere += " and NSRSBH like  '%" + NSRSBH + "%'";
            }
            if (nsrmc != "")
            {
                strwhere += " and NSRMC like  '%" + nsrmc + "%'";
            }


            BLL.Common CommonBLL = new BLL.Common();
            string str = CommonBLL.GetGetRejectTaxSqlByYear(year);
            string strwhere1 = "";
            if (str != "")
            {
                strwhere1 = @"   NOT EXISTS (SELECT  ZSXH
         FROM fund.V_SB_ZSXX s WHERE (" + str + @")  AND
                                   r.FDataSource = s.FDataSource AND
                                   r.ZSXH = s.ZSXH ) and DATEDIFF(DAY,RKRQ,'" + year + "-01-01')<=0 AND DATEDIFF(DAY,RKRQ,'" + year + "-12-31')>=0 ";
            }
            else
            {
                strwhere1 = @" DATEDIFF(DAY,RKRQ,'" + year + "-01-01')<=0 AND DATEDIFF(DAY,RKRQ,'" + year + "-12-31')>=0 ";
            }

            DataSet ds = GetData(strwhere, strwhere1, year);
            return GridViewExtension.ExportToXls(ExportGridViewSettings, ds.Tables[0], HttpUtility.UrlEncode("可补贴企业查询.xls"), true);
        }

        private static DataSet GetData(string strwhere, string strwhere1, string year)
        {
            BLL.VSBZSXX VSBZSXXBLL = new BLL.VSBZSXX();
            DataSet ds = VSBZSXXBLL.GetAllowanceWaitList(strwhere1, strwhere, year);
            return ds;
        }
        public ActionResult ReportViewerPartial()
        {
           // var strWhere = GetWhereString();
            var zsxx = AllRequestToData();
            MVCReportGeneratonHelper generator = new MVCReportGeneratonHelper();
            generator.CustomizeColumnsCollection += new CustomizeColumnsCollectionEventHandler(generator_CustomizeColumnsCollection);
            generator.CustomizeColumn += new CustomizeColumnEventHandler(generator_CustomizeColumn);
            XtraReport report = generator.GenerateMVCReport(OperateSession.GetFromCache("QueryAllowanceWaitList") as MVCxGridViewState, zsxx);
            //  XtraReport report = generator.GenerateMVCReport(gridViewState, dt);

            //report.PaperKind = System.Drawing.Printing.PaperKind.A4;

            this.ViewData["rpt"] = report;

            return PartialView("ReportViewerPartial");

        }
        void generator_CustomizeColumnsCollection(object source, ColumnsCreationEventArgs e)
        {

           
             for (int i = 0; i < e.ColumnsInfo.Count; i++)
            {

                if (e.ColumnsInfo[i].FieldName == "NSRBM")
                {
                    e.ColumnsInfo[i].ColumnWidth = 100;
                }
                if (e.ColumnsInfo[i].FieldName == "NSRSBH")
                {
                    e.ColumnsInfo[i].ColumnWidth =200 ;
                }
                if (e.ColumnsInfo[i].FieldName == "NSRMC")
                {
                    e.ColumnsInfo[i].ColumnWidth =350;

                }
                if (e.ColumnsInfo[i].FieldName == "se")
                {
                    e.ColumnsInfo[i].ColumnWidth = 150;
                }
                if (e.ColumnsInfo[i].FieldName == "years")
                {
                    e.ColumnsInfo[i].ColumnWidth = 80;
                }
               

            }

           
            
        }
        void generator_CustomizeColumn(object source, ControlCustomizationEventArgs e)
        {
            if (e.FieldName == "se")
            {

                XRTableCell c = e.Owner as XRTableCell;
                c.TextAlignment = TextAlignment.MiddleRight;

            }

            if (e.FieldName == "NSRSBH")
            {

                XRTableCell c = e.Owner as XRTableCell;
                c.TextAlignment = TextAlignment.MiddleLeft;

            }
            if (e.FieldName == "NSRMC")
            {

                XRTableCell c = e.Owner as XRTableCell;
                c.TextAlignment = TextAlignment.MiddleLeft;

            }
        }
        public ActionResult Print()
        {
           GetFetchString();
            // CreatReport(table);
            return View("Print");
        }
        public string GetFetchString()
        {
            string nsrmc = Fetch.Post("hidnsrmc");
            string NSRBM = Fetch.Post("hidNSRBM");
            string NSRSBH = Fetch.Post("hidNSRSBH");
            string year = Fetch.Post("hidyear");
            ViewData["nsrbm"] = nsrmc;
           ViewData["nsrsbh"] = NSRSBH;
               ViewData["nsrmc"] = nsrmc;
               ViewData["year"] = year;

            string strwhere;
            string strwhere1;
            year = filters(nsrmc, NSRBM, NSRSBH, year, out strwhere, out strwhere1);
            return year;
        }
        static GridViewSettings exportGridViewSettings;
        public static GridViewSettings ExportGridViewSettings
        {
            get
            {
                if (exportGridViewSettings == null)
                    exportGridViewSettings = CreateExportGridViewSettings();
                return exportGridViewSettings;
            }
        }

        public static GridViewSettings CreateExportGridViewSettings()
        {
            var ss = new GridViewSettings();
            ss.Name = "gvEditing";
            ss.KeyFieldName = "NSRBM";
            ss.CallbackRouteValues = new { Controller = "QueryAllowanceWaitList", Action = "QueryAllowanceWaitListPartial" };
            ss.Width = System.Web.UI.WebControls.Unit.Percentage(100);
            ss.SettingsPager.PageSize = EnterpriseFund.Common.GlobalConstant.CONSTRING_PAGESIZE;
            //ss.Columns.Add("NSRDZDAH", "纳税人电子档案号");
            ss.Columns.Add("NSRBM", "企业编码");
            ss.Columns.Add("NSRSBH", "纳税人识别号");
            ss.Columns.Add("FEnterprisePropName", "企业名称");
            ss.Columns.Add("FFinancePropName", "企业性质");
            ss.Columns.Add("NSRMC", "资金性质");
            ss.Columns.Add("se", "剔除后税款");
            ss.Columns.Add("years", "补贴年度");

            ss.SettingsExport.Styles.Header.BackColor = System.Drawing.Color.White;
            ss.SettingsExport.Styles.Header.ForeColor = System.Drawing.Color.Black;
            ss.SettingsBehavior.EnableCustomizationWindow = true;
			
            ss.Settings.ShowGroupPanel = true;
            ss.Settings.ShowFooter = true;

            ss.SettingsLoadingPanel.Text = "数据读取中...";

            ss.GroupSummary.Add(DevExpress.Data.SummaryItemType.Sum, "se").DisplayFormat = "合计:￥{0}";

            ss.ClientSideEvents.BeginCallback = "function(s,e){OnBeginCallback(s,e)}";
            ss.CommandColumn.Visible = false;

            ss.DataBinding = (sender, e) =>
            {
                ASPxGridView grid = (ASPxGridView)sender;
                for (int i = 0; i < grid.AllColumns.Count; i++)
                {
                    if (grid.AllColumns[i].HeaderStyle.HorizontalAlign == System.Web.UI.WebControls.HorizontalAlign.NotSet)
                    {
                        grid.AllColumns[i].HeaderStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
                    }
                }
            };
            ss.PreRender = ss.BeforeGetCallbackResult = (sender, e) =>
            {


                MVCxGridView gridView = sender as MVCxGridView;
                MVCxGridViewState gridViewState = new MVCxGridViewState(gridView);
                OperateSession.SetToCache(gridViewState, "QueryAllowanceWaitList");


            };
            return ss;
        }


    }
}
